/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "EventManager.h"
#include "Kernel.h"

extern Kernel* g_kernel;

#define TIME    (g_kernel->GetTime())

EventManager* EventManager::instance = new EventManager;

EventManager::EventManager(void)
{
}


/** Add a Listener */
void EventManager::AddListener( IEventListener* listener )
{
    type_ListTypes::iterator it = m_listeners.find( listener->GetListenerType() );
    if ( it != m_listeners.end() )
    {
        // already this type of listener, just add it to a list
        m_listeners[ listener->GetListenerType() ].push_back( listener );
    }
    else
    {   // a new type of listener
        type_Listeners list;
        list.push_back( listener );
        m_listeners[ listener->GetListenerType() ] = list;
    }
}
/** Remove a listener */
void EventManager::RemoveListener( IEventListener* listener )
{
    type_ListTypes::iterator iter = m_listeners.find( listener->GetListenerType() );
    if ( iter != m_listeners.end() )
    {
        // now find it in the list for deletion
        type_Listeners::iterator it = iter->second.begin();
        for ( ; it != iter->second.end(); it++ )
        {
            if ( (*it) == listener )
            {
                iter->second.erase( it );
                return;
                //delete listener; // dont delete because this is created on the stack
            }
        }
    }
}

/** Trigger an event NOW */
void EventManager::TriggerEvent( IEvent &e )
{
    type_ListTypes::iterator iter = m_listeners.find( e.GetListenerType() );
    if ( iter != m_listeners.end() )
    {
        type_Listeners::iterator it = iter->second.begin();
        for ( ; it != iter->second.end(); it++ )
        {
            (*it)->HandleEvent( &e );
        }
    }
}

/** Update, trigger any events in the queue */
void EventManager::UpdateEventQueue( float deltaTime )
{
    type_EventQueue::iterator it = m_events.begin(); 
    for( ; it != m_events.end(); )
    {
        IEvent* e = (*it);
        if ( TIME > e->GetStartTime() )
        {
            TriggerEvent( *e );
            it = m_events.erase( it );
            delete e;
        }
        else ++it;
    }
}

/** Queue an Event */
void EventManager::QueueEvent( IEvent *e )
{
    m_events.push_back( e );
}


/** Abort an event */
bool EventManager::AbortQueuedEvent( const IEvent &evt )
{
    type_EventQueue::iterator it = m_events.begin(); 
    for( ; it != m_events.end(); ++it )
    {
        IEvent* e = (*it);
        if ( (*e) == evt )
        {
            m_events.erase( it );
            return true;
        }
    }
    return false;
}
/** Empty all events */
void EventManager::EmptyQueue( bool executeAllEvents )
{
    while( !m_events.empty() )
    {
        IEvent *e = m_events.front();
        if ( executeAllEvents )
        {
            TriggerEvent( *e );
            m_events.pop_back();
        }
        delete e;
    }
}


